// order//order/orderPayDine/index.js
//获取应用实例
var wxpay = require('../../../utils/pay.js');
var util = require('../../../utils/util.js');
var app = getApp();
Page({
  data: {
    storeid: app.globalData.storeId,
    remark: '',
    payType: 0,
    tableId: '',
    tableName: '',
    cartResult: [],
    cutType: 0,//优惠方式1优惠券2折扣卡3余额
    cardInfo: {},
    cuttypeList: {},
    couponid: 0,
    couponmoney: 0,
    showCouponList: false,
    showCarditemList: false,
    cardType: 1,//1充值卡4玖玖卡5福鲤卡
    proSumMoney: 0,
    payMoney: 0,
    discount: 0,//折扣卡折扣
    carditemMoney: 0,//充值卡余额
    carditemDiscount: 0,
    carditemId: 0,
    couponList: [],
    confirmOrder: false,
    showSend: false,
    selList: '',
    orderTime: '',//订单创建时间
    universalCard: 2,//0没买过玖玖通用卡，1已买通用卡
    seleGif: true,
    saleUid: 0, //销售员id
    isInputCut: false,
    notCutMoney: '',
    coupon_isopen: 0,
    card_isopen: 0,
    carditem_isopen: 0,
    showAdress : false, // false自提, true 非自提
    consignee: '',    //联系人
    mobile: '',       //手机号    
    free: 0,//满多少免费
    packagMoney: 0,//包装费
    miniMoney: 0,//最低起送
  },

  selectNotCut: function () {
    this.setData({
      isInputCut: !this.data.isInputCut
    })
  },
  bindNotCotMoney: function (e) {
    this.setData({
      notCutMoney: e.detail.value
    })
    if (this.data.cutType > 0) {
      this.caculater();
    }
  },

  changeUseCard: function () {
    this.setData({
      seleGif: !this.data.seleGif
    })
  },

  getJJKInfo: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCardExists?uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          let universalCard = 0;
          if (res.data.Data) {
            universalCard = 1;
          }
          that.setData({
            universalCard: universalCard
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  addFood: function () {
    wx.reLaunch({
      url: '/pages/classes-new/index',
    })
  },

  // 选择优惠方式
  selectCuttype: function (e) {
    let cuttype = e.currentTarget.dataset.cuttype;
    let type = this.data.cutType;
    let showCouponList = this.data.showCouponList;

    if (cuttype == type) {
      cuttype = 0;
    }
    if (cuttype == 1) {
      showCouponList = true;
    }
    this.setData({
      cutType: cuttype,
      showCouponList: showCouponList
    })
    if (cuttype != 1) {
      this.caculater();
    }
  },
  chooseCouponNew: function (e) {
    let that = this;
    let couponid = e.currentTarget.dataset.couponid;
    let ordermoney = e.currentTarget.dataset.ordermoney;
    let couponmoney = e.currentTarget.dataset.money;
    let proSumMoney = parseFloat(that.data.proSumMoney);//商品总额
    if (proSumMoney < ordermoney) {
      wx.showToast({
        title: '不满足满减条件',
        mask: true
      })
      that.setData({
        cutType: 0,
      })
      return;
    }else {
      that.data.couponmoney = couponmoney;
      that.setData({
        couponid: couponid
      })
    }
  },
  updateCouponId: function () {
    if (this.data.couponid) {
      this.caculater();
    } else {
      wx.showToast({
        title: '请选择优惠券',
        mask: true
      })
      this.setData({
        cutType: 0
      })
    }
    this.closeCoupon();
  },
  closeCoupon: function () {
    let couponid = this.data.couponid;
    let cutType = this.data.cutType;
    if(couponid == 0){
      cutType = 0;
    }
    this.setData({
      showCouponList: false,
      cutType: cutType
    })
  },
  // 弹出选择充值卡
  selectCardItem: function () {
    this.setData({
      showCarditemList: true,
      cutType: 3
    })
  },

  chooseCarditem: function (e) {
    let cid = e.currentTarget.dataset.id;
    let itemList = this.data.cuttypeList.carditem;
    let carditemMoney= 0;
    for(let i = 0;i < itemList.length; i++){
      if(itemList[i].Cid == cid){
        carditemMoney = itemList[i].Balance;
      }
    }
    this.setData({
      carditemId: cid,
      carditemMoney: carditemMoney
    })
  },

  closeCarditem: function () {
    this.setData({
      showCarditemList: false
    })
  },

  // 选择充值卡
  updateCardItemid: function () {
    this.caculater();
    this.closeCarditem();
  },

  // 计算商品总额和应付金额
  caculater: function () {
    let that = this;
    let showAdress = that.data.showAdress;//false自提true非自提
    let cuttype = that.data.cutType;//优惠方式
    let proSumMoney = parseFloat(that.data.proSumMoney);//商品总额
    let notCutMoney = that.data.notCutMoney;//不参与优惠金额
    let payMoney = parseFloat(that.data.payMoney);//应付金额
    let discount = that.data.discount;//折扣卡折扣
    let carditemMoney = parseFloat(that.data.carditemMoney);//充值卡余额
    let carditemDiscount = parseFloat(that.data.carditemDiscount);//充值卡支付比例
    let cuttypeList = that.data.cuttypeList;
    let packMoney = parseFloat(that.data.packagMoney);//包装费
    let shipMoney = parseFloat(that.data.money);//配送费
 
    if (!showAdress) {//计算配送费
      shipMoney = 0;
    }
    notCutMoney = notCutMoney == '' ? 0 : parseFloat(notCutMoney); 
    if (notCutMoney > proSumMoney) {
      notCutMoney = proSumMoney;
      that.data.notCutMoney = notCutMoney;
    }
   
    let canCutMoney = proSumMoney - notCutMoney;  
    if (cuttype == 0) {
      payMoney = proSumMoney + packMoney + shipMoney; 
    }    

    if (cuttype == 1) {//优惠券
      let couponmoney = that.data.couponmoney;
      payMoney = canCutMoney - couponmoney + packMoney + shipMoney + notCutMoney;
    }

    if (cuttype == 2) {//折扣卡
      payMoney = canCutMoney * parseFloat(discount * 0.1) + packMoney + shipMoney + notCutMoney;
    }

    if (cuttype == 3) {//充值卡
      let carditemList = cuttypeList.carditem;
      let cutCardItemMoney = canCutMoney * parseFloat(carditemDiscount * 0.01);
      for (let i = 0; i < carditemList.length; i++) {
        if (carditemList[i].Cid == that.data.carditemId) {
          carditemMoney = carditemList[i].Balance;
          cutCardItemMoney = parseFloat(canCutMoney * carditemList[i].Discount * 0.01); //使用充值卡抵扣金额         
          if (carditemList[i].CardType == 5) {//福鲤卡
            payMoney = 0;
            if (cutCardItemMoney > carditemMoney) {//余额不足
              payMoney = parseFloat(cutCardItemMoney) - parseFloat(carditemMoney) + packMoney + shipMoney + notCutMoney;
            }
          } else {
            if (carditemMoney > cutCardItemMoney) {//余额充足
              payMoney = proSumMoney - parseFloat(cutCardItemMoney) + packMoney + shipMoney + notCutMoney;
            } else {
              payMoney = proSumMoney - carditemMoney + packMoney + shipMoney + notCutMoney;
            }
          }
        }
      }
    }
    that.setData({
      cutType: cuttype,
      proSumMoney: parseFloat(proSumMoney).toFixed(2),
      payMoney: parseFloat(payMoney).toFixed(2)
    })
  },

  getInfo: function () {
    let that = this;
    let cardid = wx.getStorageSync("cardId");
    let supplyUid = wx.getStorageSync("supplyUid");
    wx.request({
      url: app.globalData.host + '/api/Staff/GetInfo?id=' + cardid,
      success: function (res) {
        if (res.data.Code == 0) {        
          //代卖订单,不使用优惠券，会员卡
          if(!supplyUid){
            //that.getPromition(cardid, res.data.Data.Uid);
            that.getDirectPromition(cardid, res.data.Data.Uid);
          } 
          let ship = wx.getStorageSync('shipInfo');
          if(ship == null){
            ship = {mobile: '',consignee: ''};
          }         
          console.log('ship',ship)
          
          that.caculater();
          that.setData({
            cardInfo: res.data.Data,            
            showAdress: supplyUid ?  true : false,            
            mobile: ship.mobile ? ship.mobile : '',
            consignee: ship.consignee ? ship.consignee : ''
          })         
        }
      }
    })
  },
  // 获取优惠方式
  getDirectPromition: function (cardId,createUid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/coupon/GetDirectPromotion?uid=' + app.globalData.userInfo.Uid + '&createUid=' + createUid + '&staffId=' + cardId + '&paytype=wechat',
      success: function (res) {
        if (res.data.Code == 0) {
          let data = res.data.Data;
          //优惠券
          let couponList = data.coupon ? data.coupon : [];     
          //折扣卡
          let discount = 10;          
          if (data.card) {
            discount = parseFloat(data.card.Discount * 0.1).toFixed(1);  
          }
          //卡项目
          let cardItem = {Cid:0, Balance:0, Discount:0, CardType: 0};
          if (data.carditem.length > 0) {
            for (let i = 0; i < data.carditem.length; i++) {
              let item = data.carditem[i];
              item.ExpireTime = item.ExpireTime.substr(0, 10);
              if (res.data.Data.isBuy && item.CardType == 4) {
                cardItem = item;  //已经有玖玖卡
                break;
              } else {
                if (item.Balance > cardItem.Balance) {
                  cardItem = item;
                }
              }                      
            }    
          }
          let cutType = cardItem.Cid >0 ? 3 : that.data.cutType;
          that.setData({
            cuttypeList: data,
            couponList: couponList,
            discount: discount,
            carditemMoney: cardItem.Balance,
            carditemDiscount: cardItem.Discount,
            carditemId: cardItem.Cid,
            cardType: cardItem.cardType,
            cutType: cutType,
            coupon_isopen: data.coupon ? 1:0,
            card_isopen: data.card ? 1 : 0,
            carditem_isopen: cardItem.Cid >0 ? 1 : 0
          })

          setTimeout(function () {
            that.caculater();
          }, 1000)
        } else {
          util.showModal(res.data.Message);         
        }
      }
    })
  },
  // getCutType: function (createUid) {
  //   let that = this;
  //   wx.request({
  //     url: app.globalData.host + '/api/coupon/GetDirectPromotion?uid=' + app.globalData.userInfo.Uid + '&createUid=' + createUid + '&paytype=wechat&promotionType=0',
  //     success: function (res) {
  //       if (res.data.Code == 0) {
  //         let cuttypeList = res.data.Data;
  //         let discount = 10;
  //         let carditemMoney = 0;
  //         let carditemDiscount = 0;
  //         let carditemId = 0;
  //         let cardType = 1;
  //         let couponList = [];
  //         if (cuttypeList.card) {
  //           discount = parseFloat(cuttypeList.card.Discount * 0.1).toFixed(1);
  //         }
  //         if (cuttypeList.carditem.length > 0) {
  //           let date = new Date();
  //           let cardItemInfo = cuttypeList.carditem[0];
  //           for (let i = 0; i < cuttypeList.carditem.length; i++) {
  //             var date2 = new Date(Date.parse(cuttypeList.carditem[i].ExpireTime.replace(/-/g, "/")));
  //             if (date2 < date) { //过期
  //               cuttypeList.carditem[i].isExpiretime = 1;
  //             } else {
  //               cuttypeList.carditem[i].isExpiretime = 0;
  //             }
  //             cuttypeList.carditem[i].ExpireTime = cuttypeList.carditem[i].ExpireTime.substr(0, 10);
  //           }
  //           for (let i = 0; i < cuttypeList.carditem.length; i++) {
  //             if (res.data.Data.isBuy && cuttypeList.carditem[i].CardType == 4) {//已经有玖玖卡
  //               cardItemInfo = cuttypeList.carditem[i];
  //               that.setData({
  //                 carditem_isopen: 1,
  //               })
  //               break;
  //             } else {
  //               if (cuttypeList.carditem[i].Balance > cardItemInfo.Balance) {
  //                 cardItemInfo = cuttypeList.carditem[i];
  //               }
  //             }
  //           }
  //           that.setData({
  //             cutType: 3
  //           })
  //           carditemMoney = cardItemInfo.Balance;
  //           carditemDiscount = cardItemInfo.Discount;
  //           carditemId = cardItemInfo.Cid;
  //           cardType = cardItemInfo.CardType;
  //         }
  //         if (cuttypeList.coupon.length > 0) {
  //           couponList = cuttypeList.coupon;
  //         }
  //         that.data.cardType = cardType;
  //         that.setData({
  //           cuttypeList: cuttypeList,
  //           discount: discount,
  //           carditemMoney: carditemMoney,
  //           carditemDiscount: carditemDiscount,
  //           carditemId: carditemId,
  //           couponList: couponList
  //         })
  //         setTimeout(function () {
  //           that.caculater();
  //         }, 1000)
  //       } else {
  //         util.showModal(res.data.Message);         
  //       }
  //     }
  //   })
  // },

  // getPromition: function (cardid, createuid) {
  //   let that = this;
  //   let coupon_isopen = 0;
  //   let card_isopen = 0;
  //   let carditem_isopen = 0;
  //   wx.request({
  //     url: app.globalData.host + '/api/Staff/GetPromotion?staffId=' + cardid,
  //     success: function (res) {
  //       if (res.data.Code == 0) {
  //         let items = res.data.Data;
  //         for (let i = 0; i < items.length; i++) {
  //           if (items[i].Name == "coupon") {
  //             coupon_isopen = items[i].IsOpen;
  //           }
  //           if (items[i].Name == 'discount_card') {
  //             card_isopen = items[i].IsOpen;
  //           }
  //           if (items[i].Name == "card_item") {
  //             carditem_isopen = items[i].IsOpen;
  //           }
  //         }
  //         that.setData({
  //           coupon_isopen: coupon_isopen,
  //           card_isopen: card_isopen,
  //           carditem_isopen: carditem_isopen
  //         })
  //         that.getCutType(createuid);
  //       } else {
  //         wx.showModal({
  //           title: '提示',
  //           content: res.data.Message,
  //           showCancel: false
  //         })
  //       }
  //     }
  //   })
  // },

  //获取餐桌设置
  getDiningTable: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + "/api/Restaurant/GetDiningTable?id=" + app.globalData.diningSid,
      success: function (res) {
        if (res.data.Code == 0) {
          that.setData({
            tableId: res.data.Data.TableNumber,
            tableName: res.data.Data.Name
          })
        }
      },
    })
  },

  confirmOrder: function (type = 0) {
    var that = this;
    wx.showLoading();
    var postData = {
      Uid: app.globalData.userInfo.Uid,
      Sid: app.globalData.diningSid,
      SelList: that.data.selList,
      BuyerRemark: that.data.remark,
      SaId: 0
    }

    let url = app.globalData.host + '/api/order/SubmitSessionPreOrder';
    if (that.data.payType == 1 || type == 2) {//先付款后就餐||先用餐方式已经确认下单
      url = app.globalData.host + '/api/order/SessionConfirmOrder';
    }
    
    //初始化订单
    wx.request({
      url: url,
      method: 'POST',
      data: postData,
      success: (res) => {
        wx.hideLoading();
        wx.setNavigationBarTitle({title: '待付款订单'})
        if (res.data.Code == 0) {
          var confirm = [];
          let cartResult = [];
          if (type == 1) {
            wx.showToast({title: '下单成功'})
          }
          //订单确认成功
          if (res.data.Data) {
            confirm = res.data.Data;
            cartResult = confirm.StoreOrderList[0].StoreCartInfo.CartProductList;
            for (let i = 0; i < cartResult.length; i++) {
              if (cartResult[i].OrderProductInfo.ShowImg.indexOf('ps') != -1) {
                cartResult[i].OrderProductInfo.ShowImg = util.formatAvaterByProHead(cartResult[i].OrderProductInfo.ShowImg);
              } else {
                cartResult[i].OrderProductInfo.ShowImg = util.formatAvater(cartResult[i].OrderProductInfo.ShowImg, 'https://static.711688.net.cn/upload/store/' + app.globalData.storeId + '/product/editor/');
              }
              cartResult[i].OrderProductInfo.OptionName = '';
              if (cartResult[i].OrderProductInfo.OptionIds) {
                that.getProductOptionList(cartResult[i].OrderProductInfo.OptionIds, function (name) {
                  cartResult[i].OrderProductInfo.OptionName = name;
                });
              }
            }
            setTimeout(function () {
              that.setData({
                cartResult: cartResult,
                proSumMoney: res.data.Data.AllOrderAmount,
                payMoney: res.data.Data.AllOrderAmount,
                orderTime: res.data.Data.OrderTime,
                remark: res.data.Data.BuyerRemark,
                confirmOrder: true
              });
              that.getDeliverySet()
            }, 500)
          } else {
            if (that.data.payType == 2) {
              that.confirmOrder(2);
            }
            if (that.data.payType == 1) {
              that.getCartInfo();
            }
          }
        } else {
          util.showToast(res.data.Message);          
          setTimeout(function () {
            wx.reLaunch({url: '/pages/classes-new/index'})
          }, 1500)
        }
      }
    })
  },

  closeSend: function () {
    this.setData({
      showSend: !this.data.showSend
    })
  },

  onShow: function () {
    var that = this;
    if (app.globalData.userInfo) {
      if (that.data.payType == 1) {
        that.confirmOrder();
      }
    } else {
      setTimeout(function () {
        this.onShow();
      }.bind(that), 1000)
    }
  },

  onLoad: function (e) {
    var that = this;
    that.getDiningTable();
    that.getInfo();
    let paytype = wx.getStorageSync('paytype');
    if (e.sel) {
      that.setData({
        selList: e.sel
      })
    }
    if (e.saleUid) {
      that.setData({
        saleUid: e.saleUid
      })
    }
    if (paytype == 2 && !that.data.confirmOrder) {//先用餐方式且未确认订单
      that.getCartInfo();
      wx.setNavigationBarTitle({
        title: '待确认下单',
      })
    }
    that.getJJKInfo();
    that.setData({
      payType: paytype
    })
  },
  // 获取购物车数据
  getCartInfo: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/cart/SessionIndex',
      data: {
        sid: app.globalData.diningSid
      },
      method: 'POST',
      success: function (res) {
        if (res.data.Code == 0) {
          let cartResult = [];
          if (res.data.Data.StoreCartList.length > 0) {
            cartResult = res.data.Data.StoreCartList[0].CartProductList;
            for (let i = 0; i < cartResult.length; i++) {
              cartResult[i].IsSelected = false;
              let sel = that.data.selList;
              sel = sel.split(',');
              if (sel.length > 0) {
                for (let j = 0; j < sel.length; j++) {
                  if (sel[j].substr(2, sel[j].length) == cartResult[i].OrderProductInfo.Pid) {
                    cartResult[i].IsSelected = true;
                    break;
                  }
                }
              }
              if (cartResult[i].OrderProductInfo.ShowImg.indexOf('ps') != -1) {
                cartResult[i].OrderProductInfo.ShowImg = util.formatAvaterByProHead(cartResult[i].OrderProductInfo.ShowImg);
              } else {
                cartResult[i].OrderProductInfo.ShowImg = util.formatAvater(cartResult[i].OrderProductInfo.ShowImg, 'https://static.711688.net.cn/upload/store/' + app.globalData.storeId + '/product/editor/');
              }
              cartResult[i].OrderProductInfo.OptionName = '';
              if (cartResult[i].OrderProductInfo.OptionIds) {
                that.getProductOptionList(cartResult[i].OrderProductInfo.OptionIds, function (name) {
                  cartResult[i].OrderProductInfo.OptionName = name;
                });
              }
            }
            setTimeout(function () {
              that.setData({
                cartResult: cartResult,
                payMoney: res.data.Data.OrderAmount,
                proSumMoney: res.data.Data.OrderAmount
              });              
            }, 500)
          } else {
            if (that.data.payType == 2) {
              that.confirmOrder(2);
            }
          }
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  getProductOptionList: function (optionids, callback) {
    wx.request({
      url: app.globalData.host + '/api/product/GetProductOptionList?optionIds=' + optionids,
      success: function (res) {
        if (res.data.Code == 0) {
          let name = '';
          let list = res.data.Data;
          for (let i = 0; i < list.length; i++) {
            name += list[i].OptionList[0].Name + '/';
          }
          callback(name.substr(0, name.length - 1));
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  //获取配送费用
  getDeliverySet: function () {
    let that = this;
    let recordIds = '';
    let cartResult = that.data.cartResult;
    for (let i = 0; i < cartResult.length; i++) {
      if(cartResult[i].IsSelected) {
        recordIds += recordIds !='' ? ',': '';
        recordIds += cartResult[i].OrderProductInfo.RecordId;
      }
    }
    //自提的不显示配送费
    if(recordIds=='' || !that.data.showAdress) {
      console.log('showAdress',that.data.showAdress)
      console.log('recordIds',recordIds)
      return;
    }
    
    wx.request({
      url: app.globalData.host + '/api/Ship/GetCartShip?recordIds=' + recordIds + '&address=' + that.data.cardInfo.AreaName + that.data.cardInfo.Address ,
      success: function (res) {
        if (res.data.Code == 0) {
          let item = res.data.Data.Config;           
          if (item) {         
            that.setData({              
              free: item.FreeLimit,//满多少免费
              packagMoney: item.PackMoney,//包装费
              miniMoney: item.MinLimit,//最低起送
            })
          } 
          that.setData({
            succDeset: true,
            money: res.data.Data.ShipFee,  //配送费
          })                                 
          that.caculater();
        } else {
          util.showModal(res.data.Message);          
        }        
      }
    })
  },
  // 立即付款
  createOrder: function () {
    var that = this;
    var uid = app.globalData.userInfo.Uid // 用户登录 uid
    if (!uid) {
      util.showModal('获取用户信息失败'); return;
    }
    if (that.data.payType == 2 && !that.data.confirmOrder) {
      this.setData({
        showSend: !that.data.showSend
      })
      return;
    }
    let sellist = '';
    let cartResult = that.data.cartResult;
    for (let i = 0; i < cartResult.length; i++) {
      if (cartResult[i].IsSelected) {
        sellist += '0_' + cartResult[i].OrderProductInfo.Pid + ',';
      }
    }
    var remark = that.data.remark; // 备注信息
    let notCutMoney = that.data.notCutMoney;
    if (!notCutMoney) {
      notCutMoney = 0;
    }
    let proSumMoney = parseFloat(that.data.proSumMoney);
    let CardDiscountMoney = 0;
    let cutType = that.data.cutType;
    if (cutType == 2) {
      CardDiscountMoney = (proSumMoney - notCutMoney) * (1 - that.data.discount * 0.1);
    }
      
    var postData = {
      SaId: 0,
      Jump: 3,
      Uid: app.globalData.userInfo.Uid,
      CreateUid: that.data.cardInfo.Uid,
      Sid: app.globalData.diningSid,
      BuyerRemark: remark,
      SelList: sellist,
      CardDiscountMoney: parseFloat(CardDiscountMoney).toFixed(2),
      CardItemId: cutType == 3 ? that.data.carditemId : 0,
      NotInMoney: notCutMoney,
      CouponIdList: that.data.couponid,
      SaleUid: that.data.saleUid,
    }
    //供货商家id
    let supplyUid = wx.getStorageSync('supplyUid');    
    if(supplyUid){
      postData.Jump = 12;   //代卖扫码点餐
      postData.CreateUid = supplyUid;    
      postData.Consignee = that.data.consignee; // 联系人
      postData.Mobile = that.data.mobile;       // 手机号

      if (!util.vailPhone(that.data.mobile)) {
        util.showModal('请输入正确的手机号！'); return;
      }
    }
    if(that.data.miniMoney > that.data.payMoney){
      util.showModal('实付金额满' + that.data.miniMoney + '元配送'); return;
    }

    wx.showLoading({title: '提交中',mask: true})
    wx.setStorageSync('shipInfo',{
      mobile: that.data.mobile,
      consignee: that.data.consignee
    });
    wx.request({
      url: app.globalData.host + '/api/order/SubmitSessionOrder',
      method: 'POST',
      data: postData, 
      success: (res) => {
        wx.hideLoading();
        if (res.data.Code == 302) {
          util.showToast(res.data.Message);        
          setTimeout(function () {
            wx.reLaunch({url: '/pages/classes-new/index'})
          }, 1500)
        }
        else if (res.data.Code != 0) {        
            util.showModal(res.data.Message);        
        } else {
          let oid = res.data.Data;
          let receiveCard = 0;
          let tagindex = 4;       
          let url = '/merchant/merchants/myCardCoupon/index?tagindex=' + tagindex;
          let universalCard = that.data.universalCard;
          if (universalCard == 0 && that.data.seleGif) {
            receiveCard = 1;
          }
          //代卖订单
          if(supplyUid){
            wxpay.wxpay(app, oid, 2, url, receiveCard);
            return;
          }
          //有传单礼包
          that.getSaleInfo(that.data.cardInfo.Uid, function (count) {            
            if (count > 0 ) {
              url = '/merchant/flyer/receiveLeafletCoupon/index?createUid=' + that.data.cardInfo.Uid + '&tagindex=' + tagindex + '&showWxcard=' + receiveCard;
            }     
            wxpay.wxpay(app, oid, 2, url, receiveCard);       
          })
        }
      }
    })
  },
  // 是否含有传单礼包
  getSaleInfo: function (createUid, callback) {
    wx.request({
      url: app.globalData.host + '/api/leaflet/GetPackageTotal?uid=' + app.globalData.userInfo.Uid + '&shareUid=' + createUid,
      success: function (res) {
        if (res.data.Code == 0) {
          callback(res.data.Data.StaffCount)
        } else {
          callback(0)
        }
      }
    })
  },

  sureSend: function () {
    this.confirmOrder(1);
    this.setData({
      showSend: !this.data.showSend
    })
  },

  bindremark: function (e) {
    this.setData({
      remark: e.detail.value
    })
  },
  bindConsignee: function (e) {
    this.setData({
      consignee: e.detail.value
    })
  },
  bindMobile: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },
})
